from tkinter import *

def run():
    if CheckVar1.get() == 0 and CheckVar2.get() == 0 and CheckVar3.get() == 0 and CheckVar4.get() == 0:
        s = '您还没选择任何爱好的球类项目:'
    else:
        s1 = '旅游' if CheckVar1.get() == 1 else ""
        s2 = '运动' if CheckVar2.get() == 1 else ""
        s3 = '美食' if CheckVar3.get() == 1 else ""
        s4 = '上网' if CheckVar4.get() == 1 else ""
        s = "您选择了%s %s %s %s" % (s1, s2, s3, s4)
        lb3.config(text=s)

def reset():
    s = '重新选择爱好的球类项目'
    ch1.deselect()
    ch2.deselect()
    ch3.deselect()
    ch4.deselect()
    lb3.config(text=s)

root = Tk()
root.title('复选框的使用')

lb1 = Label(root, text='个人爱好', font=('', 32), width=20)
lb1.pack()

lb2 = Label(root, text='请从下列爱好中选择', anchor='w')
lb2.pack(fill='x')

# 创建一个 Frame 来容纳复选框
checkbox_frame = Frame(root)
checkbox_frame.pack(pady=10)

CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()
CheckVar4 = IntVar()

# 将复选框放置在 checkbox_frame 中
ch1 = Checkbutton(checkbox_frame, text='旅游', variable=CheckVar1, onvalue=1, offvalue=0, anchor='w')
ch2 = Checkbutton(checkbox_frame, text='运动', variable=CheckVar2, onvalue=1, offvalue=0, anchor='w')
ch3 = Checkbutton(checkbox_frame, text='美食', variable=CheckVar3, onvalue=1, offvalue=0, anchor='w')
ch4 = Checkbutton(checkbox_frame, text='上网', variable=CheckVar4, onvalue=1, offvalue=0, anchor='w')

ch1.grid(row=0, column=0, sticky='w', padx=10)
ch2.grid(row=1, column=0, sticky='w', padx=10)
ch3.grid(row=0, column=1, sticky='w', padx=10)
ch4.grid(row=1, column=1, sticky='w', padx=10)

# 创建一个 Frame 来容纳按钮
button_frame = Frame(root)
button_frame.pack(pady=20)

# 创建按钮并放置在 button_frame 中
bt1 = Button(button_frame, text='提交', command=run)
bt2 = Button(button_frame, text='重置', command=reset)
bt1.pack(side=LEFT, padx=10)
bt2.pack(side=RIGHT, padx=10)

lb3 = Label(root, text='')
lb3.pack()

root.mainloop()